﻿package com.rbac.system.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * 日志工具类
 * 提供统一的日志记录接口
 */
@Component
public class LogUtil {
    
    /**
     * 获取指定类的Logger
     */
    public static Logger getLogger(Class<?> clazz) {
        return LoggerFactory.getLogger(clazz);
    }
    
    /**
     * 获取指定名称的Logger
     */
    public static Logger getLogger(String name) {
        return LoggerFactory.getLogger(name);
    }
    
    /**
     * 记录业务日志
     */
    public static void business(String message, Object... params) {
        Logger logger = LoggerFactory.getLogger("BUSINESS");
        logger.info(message, params);
    }
    
    /**
     * 记录业务错误日志
     */
    public static void businessError(String message, Throwable throwable) {
        Logger logger = LoggerFactory.getLogger("BUSINESS");
        logger.error(message, throwable);
    }
    
    /**
     * 记录系统日志
     */
    public static void system(String message, Object... params) {
        Logger logger = LoggerFactory.getLogger("SYSTEM");
        logger.info(message, params);
    }
    
    /**
     * 记录系统错误日志
     */
    public static void systemError(String message, Throwable throwable) {
        Logger logger = LoggerFactory.getLogger("SYSTEM");
        logger.error(message, throwable);
    }
    
    /**
     * 记录SQL日志
     */
    public static void sql(String message, Object... params) {
        Logger logger = LoggerFactory.getLogger("SQL");
        logger.debug(message, params);
    }
    
    /**
     * 记录性能日志
     */
    public static void performance(String operation, long executionTime) {
        Logger logger = LoggerFactory.getLogger("PERFORMANCE");
        logger.info("操作: {}, 执行时间: {}ms", operation, executionTime);
    }
    
    /**
     * 记录安全日志
     */
    public static void security(String message, Object... params) {
        Logger logger = LoggerFactory.getLogger("SECURITY");
        logger.warn(message, params);
    }
    
    /**
     * 记录安全错误日志
     */
    public static void securityError(String message, Throwable throwable) {
        Logger logger = LoggerFactory.getLogger("SECURITY");
        logger.error(message, throwable);
    }
    
    /**
     * 记录用户操作日志
     */
    public static void userOperation(String username, String operation, String result) {
        Logger logger = LoggerFactory.getLogger("USER_OPERATION");
        logger.info("用户: {}, 操作: {}, 结果: {}", username, operation, result);
    }
    
    /**
     * 记录API调用日志
     */
    public static void apiCall(String method, String url, String params, long executionTime) {
        Logger logger = LoggerFactory.getLogger("API");
        logger.info("API调用 - 方法: {}, URL: {}, 参数: {}, 执行时间: {}ms", 
                   method, url, params, executionTime);
    }
    
    /**
     * 记录API错误日志
     */
    public static void apiError(String method, String url, String params, Throwable throwable) {
        Logger logger = LoggerFactory.getLogger("API");
        logger.error("API调用失败 - 方法: {}, URL: {}, 参数: {}", method, url, params, throwable);
    }
    
    /**
     * 记录数据库操作日志
     */
    public static void database(String operation, String table, String condition) {
        Logger logger = LoggerFactory.getLogger("DATABASE");
        logger.debug("数据库操作 - 操作: {}, 表: {}, 条件: {}", operation, table, condition);
    }
    
    /**
     * 记录缓存操作日志
     */
    public static void cache(String operation, String key, String result) {
        Logger logger = LoggerFactory.getLogger("CACHE");
        logger.debug("缓存操作 - 操作: {}, 键: {}, 结果: {}", operation, key, result);
    }
    
    /**
     * 记录邮件发送日志
     */
    public static void email(String to, String subject, String result) {
        Logger logger = LoggerFactory.getLogger("EMAIL");
        logger.info("邮件发送 - 收件人: {}, 主题: {}, 结果: {}", to, subject, result);
    }
    
    /**
     * 记录短信发送日志
     */
    public static void sms(String phone, String template, String result) {
        Logger logger = LoggerFactory.getLogger("SMS");
        logger.info("短信发送 - 手机号: {}, 模板: {}, 结果: {}", phone, template, result);
    }
    
    /**
     * 记录文件操作日志
     */
    public static void file(String operation, String fileName, String result) {
        Logger logger = LoggerFactory.getLogger("FILE");
        logger.info("文件操作 - 操作: {}, 文件: {}, 结果: {}", operation, fileName, result);
    }
    
    /**
     * 记录定时任务日志
     */
    public static void schedule(String taskName, String result, long executionTime) {
        Logger logger = LoggerFactory.getLogger("SCHEDULE");
        logger.info("定时任务 - 任务: {}, 结果: {}, 执行时间: {}ms", taskName, result, executionTime);
    }
    
    /**
     * 记录启动日志
     */
    public static void startup(String message, Object... params) {
        Logger logger = LoggerFactory.getLogger("STARTUP");
        logger.info(message, params);
    }
    
    /**
     * 记录关闭日志
     */
    public static void shutdown(String message, Object... params) {
        Logger logger = LoggerFactory.getLogger("SHUTDOWN");
        logger.info(message, params);
    }
}


